home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / bs941029.tgz / bbsx-941029.tar / bbsx / life.c < prev    next >
C/C++ Source or Header  |  1994-10-29  |  3KB  |  142 lines

  1. #define _HPUX_SOURCE
  2. #define STD_LIFETIME 15
  3.  
  4. #include <stdio.h>
  5.  
  6. #include <fcntl.h>
  7. #include <pwd.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/time.h>
  12. #include <fnmatch.h>
  13.  
  14. #include "bbs.h"
  15. #include "bbs.hd"
  16.  
  17. const struct cmdtable cmdtable[1];
  18.  
  19. /*---------------------------------------------------------------------------*/
  20.  
  21. int main(int argc, char **argv)
  22. {
  23.  
  24.   FILE * fp, *fp1;
  25.   static int fdindex;
  26.   char  buf[1024];
  27.   char     line[80];
  28.   char junk[1024];
  29.   int  fd,param;
  30.   int  i,ok;
  31.   struct index index;
  32.   long now,inptime;
  33.   struct tm utctime;
  34.     struct lifetime {
  35.     char area[80];
  36.     int  lt;
  37.     struct lifetime *next;
  38.   };
  39.   
  40.   struct lifetime *lifetime, *pt, *next;
  41.   int lifetm;
  42.   int other;
  43.  
  44.   struct passwd *pw;
  45.  
  46.   if (getuid()) {
  47.     pw = getpwnam(bbsadm);
  48.     if(getuid() != pw->pw_uid) {
  49.       perror("permission denied");
  50.       return 1;
  51.     }
  52.   }    
  53.  
  54.   if (chdir(WRKDIR)) {
  55.     mkdir(WRKDIR, 0755);
  56.     if(chdir(WRKDIR)) halt();
  57.   }  
  58.  
  59.   read_config();
  60.   
  61.   if ((fdindex = open(INDEXFILE, O_RDWR, 0644)) < 0) return 1;
  62.   
  63.   pt=malloc(sizeof(*pt));
  64.   next=lifetime=pt; 
  65.   pt->next=(struct lifetime *) NULL; 
  66.   if (fp = fopen(LIFEFILE, "r")) {
  67.     while(!feof(fp)) {
  68.       fgets(line,80,fp);
  69.       param=sscanf(line,"%s %d%s",pt->area,&(pt->lt), junk);
  70.       if (param > 1 && *(pt->area) != '#') {  
  71.         strupc(pt->area);
  72.         if (next=malloc(sizeof(*pt))) {
  73.           pt->next=next; 
  74.           pt=next;
  75.           pt->next=(struct lifetime *) NULL;
  76.         } 
  77.         else {
  78.           printf("%s", "not enough memory\n");
  79.           return;
  80.         }
  81.       }  
  82.     } 
  83.     fclose(fp); 
  84.     
  85.     pt = lifetime;
  86.     while (pt->next != (struct lifetime *) NULL && 
  87.            strcmp(pt->area,"OTHER")) {
  88.       pt=pt->next;
  89.     } 
  90.     if (!strcmp(pt->area,"OTHER"))
  91.       other=pt->lt;
  92.     else
  93.       other=STD_LIFETIME;
  94.  
  95.   for (; ; ) {
  96.     if (read(fdindex, (char *) &index, sizeof(index)) != sizeof(index)) goto stop;
  97.     if (!(index.flags & DELETED)) {
  98.       lifetm= (index.lifetime_h << 8) + (index.lifetime_l);
  99.       time(&now);
  100.       inptime=(long) ((now-index.date)/86400L);
  101.       ok=1;strupc(index.to);
  102.       pt = lifetime;
  103.       while (pt->next != (struct lifetime *) NULL && ok) {
  104.         if (fnmatch(pt->area, index.to, FNM_CASEFOLD) == FNM_NOMATCH)
  105.           ok = 1;
  106.         else
  107.           ok = 0;  
  108.         if (ok) pt=pt->next;
  109.       } 
  110.       if (!ok) {
  111.         if (pt->lt)
  112.           if (lifetm > pt->lt || !lifetm)
  113.             lifetm=pt->lt;
  114.       }
  115.       if (ok)
  116.         if (lifetm > other || !lifetm)
  117.           lifetm=other;
  118.  
  119.       if (lifetm <= inptime && lifetm) {
  120.       
  121.         if (strcmp(pt->area,"B")) {
  122.           strcpy(index.to,"B");
  123.           strcpy(index.at,Myhostname);
  124.           index.date = time((long *) 0);
  125.           }                            
  126.           else {
  127.             unlink(getfilename(index.mesg));
  128.             index.flags = index.flags | DELETED;
  129.           }
  130.           if (lseek(fdindex, -sizeof(struct index), SEEK_CUR) < 0) return -1;
  131.           if (write(fdindex, &index, sizeof(struct index)) != sizeof(struct index))
  132.             return -1;
  133.         }    
  134.       }  
  135.     }  
  136.   }
  137. stop:
  138.   close(fdindex);
  139.   return 0;
  140. }
  141.  
  142.